import Vue from 'vue'
import VueRouter from "vue-router"
import Login from "./views/admin/Login"
import Index from './views/admin/Index'
import User from './views/admin/user/Index'
import Channel from './views/admin/channel/Index'
import Tag from './views/admin/tag/Index'
import FriendLink from './views/admin/friendLink/Index'
import Article from './views/admin/article/Index'
import Comment from './views/admin/comment/Index'
import Info from "./views/admin/user/Info";
import token from "./token";
import ChannelAdd from "./views/admin/channel/Add";
import ChannelEdit from "./views/admin/channel/Edit";
import ArticleAdd from "./views/admin/article/Add";
import ArticleEdit from "./views/admin/article/Edit";
import FriendAdd from './views/admin/friendLink/Add'
import FriendEdit from './views/admin/friendLink/Edit'
import UpdatePwd from "./views/admin/user/UpdatePwd";

import FIndex from './views/front/Index'
import List from "./views/front/List";
import Detail from "./views/front/Detail";
import ChannelDetail from "./views/front/ChannelDetail";
import TagList from "./views/front/TagList";

Vue.use(VueRouter)

const router = new VueRouter({
    mode: 'history',
    routes: [
        {
            path: '/',
            name: 'fIndex',
            component: FIndex,
            meta: {
                requiredLogin: false
            }
        },
        {
            path: '/list/:id',
            name: 'list',
            component: List,
            meta: {
                requiredLogin: false
            }
        },
        {
            path: '/detail/:id',
            name: 'detail',
            component: Detail,
            meta: {
                requiredLogin: false
            }
        },
        {
            path: '/channel_detail/:id',
            name: 'channel_detail',
            component: ChannelDetail,
            meta: {
                requiredLogin: false
            }
        },
        {
            path: '/tag_list/:id',
            name: 'tag_list',
            component: TagList,
            meta: {
                requiredLogin: false
            }
        },

        {
            path: '/login',
            name: 'login',
            component: Login
        },
        {
            path: '/index',
            name: 'index',
            component: Index,
            children: [
                {
                    path: '/info',
                    name: 'info',
                    component: Info
                },
                {
                    path: '/user',
                    name: 'user',
                    component: User
                },
                {
                    path: '/password',
                    name: 'password',
                    component: UpdatePwd
                },
                {
                    path: '/channel',
                    name: 'channel',
                    component: Channel
                },
                {
                    path: '/channel_add',
                    name: 'channel_add',
                    component: ChannelAdd
                },
                {
                    path: '/channel_edit',
                    name: 'channel_edit',
                    component: ChannelEdit
                },
                {
                    path: '/tag',
                    name: 'tag',
                    component: Tag
                },
                {
                    path: '/friend',
                    name: 'friend',
                    component: FriendLink
                },
                {
                    path: '/friend_add',
                    name: 'friend_add',
                    component: FriendAdd
                },
                {
                    path: '/friend_edit',
                    name: 'friend_edit',
                    component: FriendEdit
                },
                {
                    path: '/article',
                    name: 'article',
                    component: Article
                },
                {
                    path: '/article_add',
                    name: 'article_add',
                    component: ArticleAdd
                },
                {
                    path: '/article_edit',
                    name: 'article_edit',
                    component: ArticleEdit
                },
                {
                    path: '/comment',
                    name: 'comment',
                    component: Comment
                }
            ]
        }
    ]
})

//执行路由跳转之前
router.beforeEach((to, from, next) => {

    if (!to.matched.some(r => r.meta.requiredLogin)) {
        next();
    } else {
        if (token.get()) {
            next();
        } else {
            if (to.path === '/login') {
                next();
            } else {
                next('/login')
            }
        }
    }

})

export default router